/*
GENERATE EMPLOYMENT OUTCOMES

Data 	: 
Folder 	: 
Date	: 

Creator		: Jonas Cederlof	(JC)
Description :	
Notes 		:

LATSEST UPDATE : 2018-11-21
*/

********************************************************************************

clear
set more		 off
cap   log close 	_all

log using 	"../log/A2_gen_employment.log" 	, replace 
use 	lopnr year month date firmid  work lonfink  nrdisp dispid astsni* 	using "$datapath/A2_merged_RAMS_varsel.dta"



{ // Variable for main employer
*===============================================================================
preserve
	*The sorting puts the firm with the highest earnings LAST!
	sort lopnr year lonfink 
	gcollapse (last) firmid, by(lopnr year) fast
	
	gen			mainemployer 	 = firmid
	bys lopnr (year) : gen 	mainemployer_tm1 = firmid[_n-1]
	
	keep mainemployer* lopnr year
	save "$datapath/mainemployer.dta",replace
restore	
}
*
{ // Generate: Indicator for working (irrespetive of firm/employer) 
*===============================================================================
*Note: 	   Setting working`i'==0 if missing data point before ym(2016,12)
preserve
	gcollapse (max) work  ,by(lopnr date) fast

	*work variable is missing in instances where the data has been tsfilled
	*i.e data has been expanded in time in A2_merge_RAMS_varsel.do
	replace work= 0 if work==. & date<=ym(2018,12)
	
	rename work working
	lab var	working 	"==1 if working (at any firm) in given month"	
	
	keep 	lopnr date working
	save 	"$datapath/working.dta", replace
restore	
}
*
{ // Generate: Indicator for working at notifying firm 
*===============================================================================
preserve
	*Keep notifying firm
	bys lopnr : gen notifying_firm = firmid if nrdisp==dispid
	fastmax notifying_firm, by(lopnr) name(NFid)
	keep if firmid==NFid
	
	gcollapse (max) work year ,by(lopnr date) fast
	
	*work variable is missing in instances where the data has been tsfilled
	*i.e data has been expanded in time in A2_merge_RAMS_varsel.do
	replace work= 0 if work==. & date<=ym(2018,12)
	
	rename work atfirm
	lab var atfirm 		"==1 if working at notifying firm in given month"
	
	keep lopnr date atfirm
	save 	"$datapath/atfirm.dta", replace
restore

}
*	
{ // Generate: Indicator for working at other firm then notifying firm
*===============================================================================
preserve
	*Keep non-notifying firm
	bys lopnr : gen notifying_firm = firmid if nrdisp==dispid
	fastmax notifying_firm, by(lopnr) name(NFid)
	keep if firmid!=NFid
	
	
	gcollapse (max) work year ,by(lopnr date) fast
	replace work= 0 if work==. & date<=ym(2018,12)
	
	rename work otherfirm
	lab var otherfirm 		"==1 if working at OTHER firm in given month"
	
	keep lopnr date otherfirm
	save 	"$datapath/otherfirm.dta", replace
restore

}
*

{ // Generate: Employer id for all different firms each month the worker is employed at
*===============================================================================
preserve
	bys lopnr : gen notifying_firm = firmid if nrdisp==dispid
	fastmax notifying_firm, by(lopnr) name(NFid)
	keep if firmid!=NFid
	keep if work==1
	
	sort lopnr firmid year date lonfink // sort so you take the highest paying plant within firm
	fcollapse (sum) lonfink work (last) astsni* ,by(lopnr firmid year date) fast
	
	gen neglonfink = -lonfink
	bys lopnr date (neglonfink): gen nvals = _n
	tab nvals
	drop if nvals>10
	
	replace firmid = 0 if firmid==. // odd that 48 firmids are missing...
	bys lopnr firmid year (date) : egen sumwork =total(work)
	gen Eear = lonfink/sumwork
	rename firmid E
	gen sni02 = substr(astsni02,1,3)
	gen sni07 = substr(astsni07,1,3)
	rename sni02 Esni02
	rename sni07 Esni07
	
	
	drop neglonfink work sumwork lonfink astsni*
	
	reshape wide E Eear Esni07 Esni02 , i(lopnr date) j(nvals)
	
	forvalues i = 1(1)10{
		lab var E`i' 		"ID: Otherfirm firmid (not notifying firm) where work==1 (ranked on pay 1=highest earnings) "
		lab var Eear`i' 	"Earnings/monthsworked from from E`i'"
		lab var Esni02`i'	"SNI 2002 (3-digit) for E`i'"
		lab var Esni07`i'	"SNI 2007 (3-digit) for E`i'"
	}
	
	*Note: E*==. if work==0 (i.e the E variable only exists when the employee is working at the firm)
	keep lopnr date E* Eear* Esni*
	save 	"$datapath/otherfirm_firmids.dta", replace
restore

}
*
	
	
log close
